import React from 'react'
// ===================================================================== Temp
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Checkbox 	=  $Lazy.load(()=>import('@antd/form/checkbox'))
const $cache 	=  $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		// 提交表单列表
		formData : [
			{ label: '', name: 'pathological_diagnosis', itemClassName:'checkbox-groups', full:true, type:'checkboxGroup', idStr:'name', nameStr:'name', onValue:({value})=>{
				this.setState({ len: value.length, allChecked: value.length === this.checkData.length })
			}, onChecked:(value, checked)=>{
				setTimeout(()=>{
					if(checked){
						if(!this.checked.includes(value)){
							this.checkedMore.push(value)
						}
					}else{
						this.checkedItem.forEach(v=>{
							const index = v.findIndex(v=> v === value )
							index !== -1 && v.splice(index, 1)
						})
					}
				})
			} },
		],
		okDisabled: true
	}
	checkData = []
	checked = []
	checkedItem = []
	checkedMore = []
	arrData = []
	// 获取弹框下拉
	getModalSelect = async callback => {
		const { formData } = this.state
		await $fn.setModalRows(formData, 'pathological_diagnosis', async ()=>{
			await $cache.then( async f => {
				// 获取设备名称
				this.checkData = $fn.setRow( formData, 'pathological_diagnosis', await f.getDisCache({ code: 67000}))
				//
				this.setState({ formData })
			})
		})
	}
	
	open = async () => {
		this.checkedMore = []
		this.modal.open({ formRef: this.formRef })
		await this.getModalSelect()
		const v = this.props.form.current.refs.ref.getName('pathological_diagnosis')
		this.formRef.reset()
		
		this.setState(({ len })=>{
			if($fn.isValid(v)){
				// let arr =  v.split(',')
				let arr =  v.split(/\n|\r\n/)
				arr.forEach((v)=>{
					let a = v.split('、')
					!$fn.isEmpty(v) && this.checkedItem.push(a)
				})
				this.checkedItem.forEach((v)=>{
					if($fn.hasArray(v)) {
						this.checked = [...this.checked, ...v]
					}
				})
				this.arrData = []
				this.checkData.forEach(({name}) => {
					this.checked.forEach(v => v === name && this.arrData.push(v) )
				})
				this.formRef.setValue({ pathological_diagnosis: this.checked })
			
				return { len: this.arrData.length, allChecked: this.arrData.length === this.checkData.length  }
			}else{
				return { len: 0, allChecked:false }
			}
		})
	}
	
	render(){
		const { len, allChecked } = this.state
		return FormModal.call(this,{ title:'免疫组化抗体', width:850,
			okDisabled: len === 0,
			prefix: (
				<div className='fxm b bbor1 pb10 mb10'>
					<Checkbox label='全选' outer value={allChecked} onChange={ value =>{
						const all = this.checkData.map(v=>v.name)
						if(value){
							this.formRef.reset()
							this.formRef.setValue({pathological_diagnosis: all})
							this.checked = all
							all.forEach((value)=>{
								this.checkedItem.forEach(v=>{
									const index = v.findIndex(v=> v === value )
									index !== -1 && v.splice(index, 1)
								})
							})
							this.checkedItem = [...this.checkedItem, all]
							this.setState({ len: all.length, allChecked: value })
						}else{
							this.formRef.setValue({pathological_diagnosis:[]})
							this.checked = []
							all.forEach((value)=>{
								this.checkedItem.forEach(v=>{
									const index = v.findIndex(v=> v === value )
									index !== -1 && v.splice(index, 1)
								})
							})
							
							this.setState({ len: 0, allChecked: value })
						}
					}} />
					<div className='b f13 fxm'>选中<span className='mlr5 f14 c0'>{this.state.len||0}</span>条</div>
				</div>
			),
			onSubmit: ({ pathological_diagnosis }) => {
				// 病理诊断显示标点符号处理 2021-10-21  --马婷 
				// this.props.form.current.refs.ref.setValue({pathological_diagnosis: this.checked.join(',')})
				if($fn.hasArray(this.checkedMore)){
					this.checkedItem = [...this.checkedItem,this.checkedMore]
				}
				let str = ''
				this.checkedItem.forEach((v)=>{
					if($fn.hasArray(v)){
						str += v.join('、') + '\n'
					}
				})
				this.props.form.current.refs.ref.setValue({pathological_diagnosis: str})
				this.checked = []
				this.checkedMore = []
				this.checkedItem = []
				this.arrData = []
				this.modal.close()
			}
		})
	}
}

export default Index